package com.example.concurrency.chapter10;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTests {
    private static final int workerNum = 2;
    private static CountDownLatch latch = new CountDownLatch(workerNum);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < workerNum; i++){
            new Thread(new Task()).start();
        }
        System.out.println("等待子线程执行完成");
        latch.await();
        System.out.println("主线程完成");
    }

    static class Task implements Runnable{
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "：执行完成");
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
